package com.happyscan138.makefun.Fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.happyscan138.makefun.BaseAdapter.my_friend;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.adapter.myFriendAdapter;
import com.happyscan138.makefun.base.http;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static android.content.Context.MODE_PRIVATE;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/12/5.
 */

public class myfriend_fans extends Fragment {
    private List<my_friend> ListData;
    private ListView listView_myfriend;
    //声明一个SharedPreferences对象和一个Editor对象
    private SharedPreferences preferences;
    private String access_key;
    private my_friend data;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//
        View view = inflater.inflate(R.layout.myfriend_fans, null);
        preferences = getActivity().getSharedPreferences("UserInfo", MODE_PRIVATE);
        access_key=preferences.getString("UserId","");
        http.client=new AsyncHttpClient();
        http.params=new RequestParams();
        initView(view);
        getAdapterView(view);
        return view;
    }

    public void initView(View view) {
        listView_myfriend = (ListView) view.findViewById(R.id.my_friend_my_fans);
    }

    public View getAdapterView(View view) {
        http.params.put("access_key",access_key);

        RequestHandle handle= http.client.post(Urls.FansList, http.params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                String jsonStr=response.toString();
                ListData=new ArrayList<>();
                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject objectData=jsonArray.getJSONObject(i);
                        data=new my_friend();
                        data.head= Urls.base+objectData.getString("img_path");
                        log.e("head",data.head.toString());
                        data.name=objectData.getString("nick_name");
                        log.e("name",data.name.toString());
                        data.focus_id=objectData.getString("focus_id");
                        ListData.add(data);
                        log.e("listdata",ListData.toString());
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                listView_myfriend.setAdapter(new myFriendAdapter(getActivity(), ListData,access_key));
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
            }
        });

        return view;
    }
}